package com.flyou.ccnucenter.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.Lost;
import com.flyou.ccnucenter.utils.FrescoUtils;

import java.util.List;

/**
 * ============================================================
 * 项目名称：HenuCenterPro
 * 包名称：com.flyou.henucenter.adapter
 * 文件名：NoteRecycleViewAdapter
 * 类描述：
 * 创建人：flyou
 * 邮箱：fangjaylong@gmail.com
 * 创建时间：2015/12/26 15:03
 * 修改备注：
 * 版本：@version  V1.0
 * ============================================================
 **/
public class HenuBesideLostRecycleViewAdapter extends RecyclerView.Adapter<HenuBesideLostRecycleViewAdapter.MyViewHolder> {
    private Context context;
    private List<Lost> losts;

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public HenuBesideLostRecycleViewAdapter(Context context, List<Lost> losts) {
        this.context = context;
        this.losts = losts;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.view_lost_list_item, null));

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.title.setText(losts.get(position).getTitle());
        holder.time.setText(losts.get(position).getCreatedAt());
       String[] imageUrls = losts.get(position).getImageUrls();
        if (null != imageUrls && imageUrls.length > 0) {
            FrescoUtils.setImageUrlWithSize(holder.image,imageUrls[0],200,200);
        } else {
            holder.image.setImageURI(Uri.parse(""));
        }
        switch (losts.get(position).getStatus()) {
            case Lost.FOUND_BEING:
                holder.status.setBackgroundResource(R.drawable.bbs_icon_yellow);
                holder.status.setText("招领中");
                break;
            case Lost.LOST_BEING:
                holder.status.setBackgroundResource(R.drawable.activity_blue);
                holder.status.setText("寻物中");
                break;
            case Lost.CLOSE:
                holder.status.setText("已关闭");
                holder.status.setBackgroundResource(R.drawable.activity_gray);
                break;
            case Lost.FOUND:
                holder.status.setText("已找回");
                holder.status.setBackgroundResource(R.drawable. activity_green);
                break;
        }

        if (losts.get(position).isHide().equals("true")){
            holder.status.setText("已经隐藏");
            holder.status.setBackgroundResource(R.drawable.activity_gray);
        }


        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return losts.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        TextView time;
        TextView status;
        SimpleDraweeView image;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.tv_lost_title);
            time = (TextView) view.findViewById(R.id.tv_lost_createTime);
            status = (TextView) view.findViewById(R.id.lost_status);
            image = (SimpleDraweeView) view.findViewById(R.id.lost_image);


        }
    }

    public void addItem(Lost lost) {

        losts.add(lost);
        notifyItemInserted(0);
    }

    public void removeItem(int position) {
        losts.remove(position);
        notifyItemRemoved(position);
    }
}
